package com.jason.kotlin.activityresult

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContract

/**
 * Description:MyActivityResultContract
 *
 * @author chenbaoyang
 * @create 2021/1/14 14:45
 */
class MyActivityResultContract : ActivityResultContract<String, String>() {

    override fun createIntent(context: Context, input: String?): Intent {
        return Intent(context,SecondActivity::class.java).apply {
            putExtra("name",input)
        }
    }

    override fun parseResult(resultCode: Int, intent: Intent?): String? {
        val data = intent?.getStringExtra("result")
        return if (resultCode == Activity.RESULT_OK && data != null) data
        else null
    }
}

class MyActivityResultContract2(private val clazz: Class<out Any>) : ActivityResultContract<Bundle, Intent>() {

    override fun createIntent(context: Context, input: Bundle?): Intent {
        return Intent(context, clazz).apply {
            if (input != null) {
                putExtras(input)
            }
        }
    }

    override fun parseResult(resultCode: Int, intent: Intent?): Intent? {
        return intent
    }
}